package com.sci.controller;

import com.sci.dto.CreateOrderRequest;
import com.sci.dto.OrderDTO;
import com.sci.entity.OrderRow;
import com.sci.entity.Orders;
import com.sci.entity.User;
import com.sci.feign.UserServiceClient;
import com.sci.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private UserServiceClient userServiceClient;
    @Resource
    private OrderService orderService;



    // OrderController.java
    @GetMapping("/list/{wechatId}")
    public List<OrderDTO> getOrderDTObyWechatId(@PathVariable(value = "wechatId") String wechatId) {
        System.out.println("wechatId: " + wechatId);
        return orderService.getOrderDTObyWechatId(wechatId); // 调用新方法
    }

    @PostMapping("/create")
    public Boolean createOrder(@RequestBody CreateOrderRequest request) {
        // 生成订单ID（示例：UUID）
        String orderId = "ORD" + UUID.randomUUID().toString().replace("-", "").substring(0, 10).toUpperCase();

        // 构建Orders对象
        Orders order = new Orders();
        order.setOrderId(orderId);
        order.setBusinessId(request.getBusinessId());
        order.setPhone(request.getPhone());
        order.setNote(request.getNote());
        order.setAddress(request.getAddress());
        order.setStatus(0); // 默认状态

        // 转换OrderRows
        List<OrderRow> orderRows = request.getOrderRows().stream()
                .map(item -> {
                    OrderRow row = new OrderRow();
                    row.setOrderId(orderId);
                    row.setFoodId(item.getFoodId());
                    row.setQuantity(item.getQuantity());
                    return row;
                }).collect(Collectors.toList());

        // 调用Service层
        return orderService.createOrder(order, orderRows);
    }


}
